package com.hornsun.repository;

import com.google.common.base.Preconditions;
import com.hornsun.domain.dbo.Coupon;
import com.hornsun.domain.dbo.MemberCoupon;
import com.hornsun.domain.dbo.MemberCouponExample;
import com.hornsun.domain.dto.CouponRecordDTO;
import com.hornsun.repository.mapper.CommonMapper;
import com.hornsun.repository.mapper.MemberCouponMapper;
import com.hornsun.util.LittleUtil;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by 编译中... on 2018/3/19.
 */
@Repository
public class MemberCouponRepository {

    @Resource
    private MemberCouponMapper memberCouponMapper;

    @Resource
    private LittleUtil littleUtil;

    @Resource
    private CommonMapper commonMapper;


    /**
     * 可使用的优惠券：用户有的（openid不为空）+没有使用过的（payTime为空且orderGuid为空）+时间没过期的（过期时间大于当前时间）
     * @param openid
     * @return
     */
    public List<CouponRecordDTO> queryUsableCoupon(String openid, int page){
        int offset = (page-1)*8;
        return commonMapper.getUsableCoupon(openid,offset);
    }

    /**
     * 已使用的优惠券：用户有的+有使用时间的（payTime不为空）
     * @param openid
     * @return
     */
    public List<CouponRecordDTO> queryUsedCoupon(String openid, int page){
        int offset = (page-1)*8;
        return commonMapper.getUsedCoupon(openid,offset);
    }

    /**
     * 已过期的优惠券：用户有的+没使用过的+过期时间小于当前时间的
     * @param openid
     * @return
     */
    public List<CouponRecordDTO> queryOverdueCoupon(String openid, int page){
        int offset = (page-1)*8;
        return commonMapper.getOverdueCoupon(openid,offset);
    }

    /**
     *
     */
    public  List<CouponRecordDTO> queryUsableCouponByType(String openid , Byte type){
        return commonMapper.getUsableCouponByType(openid,type);
    }

    /**
     * 可使用的优惠券的数量
     * @param openid
     * @return
     */
    public long usableCouponCount(String openid){
        MemberCouponExample memberCouponExample = new MemberCouponExample();
        MemberCouponExample.Criteria criteria = memberCouponExample.createCriteria();
        criteria.andOpenidEqualTo(openid).andPayTimeIsNull().andOrderNumIsNull().andExpirationTimeGreaterThan(new Date());
        return memberCouponMapper.countByExample(memberCouponExample);
    }

    /**
     * 已使用的优惠券的数量
     * @param openid
     * @return
     */
    public long UsedCouponCount(String openid){
        MemberCouponExample memberCouponExample = new MemberCouponExample();
        MemberCouponExample.Criteria criteria = memberCouponExample.createCriteria();
        criteria.andOpenidEqualTo(openid).andPayTimeIsNotNull();
        return memberCouponMapper.countByExample(memberCouponExample);
    }

    /**
     * 过期的优惠券的数量
     * @param openid
     * @return
     */
    public long OverdueCouponCount(String openid){
        MemberCouponExample memberCouponExample = new MemberCouponExample();
        MemberCouponExample.Criteria criteria = memberCouponExample.createCriteria();
        criteria.andOpenidEqualTo(openid).andPayTimeIsNull().andExpirationTimeLessThan(new Date());
        return memberCouponMapper.countByExample(memberCouponExample);
    }

    /**
     *用户获取优惠券
     * @param memberCoupon
     */
    public void addMemberCoupon(MemberCoupon memberCoupon){
        int count = memberCouponMapper.insertSelective(memberCoupon);
        if (count!=1){
            throw new IllegalArgumentException("insert memberCoupon failed by memberCoupon:"+memberCoupon);
        }
    }

    /**
     * 删除过期半年以上
     */
    public void deleteInvalidMemberCoupon(){
        Date time =new Date();
        time.setTime(time.getTime()-183*24*60*60*1000);
        MemberCouponExample memberCouponExample = new MemberCouponExample();
        MemberCouponExample.Criteria criteria = memberCouponExample.createCriteria();
        criteria.andExpirationTimeLessThan(time);
        memberCouponMapper.deleteByExample(memberCouponExample);
    }

    /**
     * 获取已发送的条目数
     * @param coupon
     * @return
     */
    public long querySendCount(Coupon coupon){
        MemberCouponExample memberCouponExample = new MemberCouponExample();
        MemberCouponExample.Criteria criteria = memberCouponExample.createCriteria();
        criteria.andCouponGuidEqualTo(coupon.getCouponGuid());
        return memberCouponMapper.countByExample(memberCouponExample);
    }

    /**
     * 获取已使用优惠券
     * @param coupon
     * @return
     */
    public long queryUsedCount(Coupon coupon){
        MemberCouponExample memberCouponExample = new MemberCouponExample();
        MemberCouponExample.Criteria criteria = memberCouponExample.createCriteria();
        criteria.andCouponGuidEqualTo(coupon.getCouponGuid()).andPayTimeIsNotNull();
        return memberCouponMapper.countByExample(memberCouponExample);
    }

    /**
     * 使用优惠券
     * @param memberCoupon
     */
    public void updateMemberCoupon(MemberCoupon memberCoupon){
        int count = memberCouponMapper.updateByPrimaryKeySelective(memberCoupon);
        if (count!=1){
            throw new IllegalArgumentException("update memberCoupon failed by memberCoupon:"+memberCoupon);
        }
    }

    /**
     * 释放优惠券（令payTime跟orderGuid为null）
     */
    public void updateToUsable(String memberCoupon){
        memberCouponMapper.updatePayTime(memberCoupon);
    }



    /**
     * 查询优惠券
     * @param memberCouponGuid
     * @return
     */
    public MemberCoupon getMemberCouponByGuid(String memberCouponGuid){
        Preconditions.checkNotNull(memberCouponGuid);
        return memberCouponMapper.selectByPrimaryKey(memberCouponGuid);
    }



}
